package server;

import java.io.*;
import java.net.Socket;
import java.util.Date;

public class HandleConn extends Thread {
    Socket socket;
    public int clientId=-1;
    public boolean rushable=false;//现在记录的是 是否再接受rush信息，当已接受后设置为false
    public long rushtime=-1;//rush的时间,-1是初始值,-2是犯规标志
    public HandleConn(Socket socket){
        this.socket=socket;
    }
    public void run(){
        while(true){
            try {
                BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
                Out.say("wait for msg.");
                StringBuffer msg = new StringBuffer(bufferedReader.readLine());

                Out.say("client:"+clientId+" new msg:"+msg);
                //String line=null;
                // 进行输出
                //while (( line= bufferedReader.readLine()) != null) {
                    //System.out.println("-----客户端输入的信息是-----");
                    //System.out.println(line);

                //}
                //Out.say("msg:"+msg.toString());
                String cmd[]=msg.toString().split(" ");
                switch(cmd[0]){
                    case "id":{
                        this.clientId=Integer.parseInt(cmd[1]);
                        Out.say("conn:IP:"+this.socket.getInetAddress()+" 's id is "+this.clientId);
                        Out.say("reply server time.");
                        BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                        bufferedWriter.write("time "+new Date().getTime());
                        bufferedWriter.newLine();
                        bufferedWriter.flush();
                        ServerMain.mf.repaint();
                        break;
                    }
                    case "rush":{//抢答信息
                        if(CheckConn.acceptRush){
                            if(this.rushable) {//抢答成功
                                //CheckConn.acceptRush = false;
                                this.rushable=false;
                                this.rushtime=Long.parseLong(cmd[1]);
                                //检查是否犯规
                                if(ServerMain.state==ServerMain.COUNT||this.rushtime<CheckConn.startTime){
                                    this.rushtime=-2;
                                    ServerMain.mf.repaint();
                                    Out.say("client "+clientId+" rush illegally.");
                                }
                                //ServerMain.mf.rushSuccess(clientId);
                            }/*else if(this.rushable&&ServerMain.state==ServerMain.COUNT){//犯规，设置可抢答为false
                                //this.rushable=false;
//                                BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
//                                bufferedWriter.write("illegal");//报告客户端犯规
//                                bufferedWriter.newLine();
//                                bufferedWriter.flush();
                            }*/else {
                                ;//空闲状态啥也不做
                                //错，空闲状态进不来这个if
                            }
                        }
                        break;
                    }
                    case "time":{
//                        BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));
//                        bufferedWriter.write("time "+new Date().getTime());
//                        bufferedWriter.newLine();
//                        bufferedWriter.flush();
                        //bufferedWriter.close();
                        //Out.say("time!!!!!!!");
                        break;
                    }
                }
            } catch (Exception e) {
                Out.say("[ERR]read conn failed:"+e.getStackTrace());
                //e.printStackTrace();
                ServerMain.checkConn.conns.remove(this);
                Out.say("Client thread:"+clientId+" stop");
                ServerMain.mf.repaint();
                this.stop();
            }
        }
    }
}
